﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.CodeDom;
using Wicresoft.EKA.NRemedy.CodeGenerate;

namespace Wicresoft.EKA.NRemedy.CodeGenerator.WinForm
{
    public class CodeGenerateThread : ARThread
    {
        private string input = string.Empty;
        private string namespacem = string.Empty;

        public PushCodeDelegate GenerateCompeleted;

        public CodeGenerateThread()
        {
        }

        public void Start(params object[] param)
        {
            _thread = new Thread(GenerateCode);
            input = param[0].ToString();
            namespacem = param[1].ToString();
            _thread.Start();
        }


        private void GenerateCode()
        {
            string code = string.Empty;
            string error = string.Empty;
            CSharpModelGenerator generator = new CSharpModelGenerator(_context);
            CSharpModelGeneratorDefaultFactory csmgdf = new CSharpModelGeneratorDefaultFactory();
            CodeCompileUnit ccu;
            try
            {
                ccu = generator.GenerateClassByFormName(namespacem, input, GlobalCache.FieldIDSelected, csmgdf);
                code = generator.GenerateCodeByCodeCompileUnit(ccu);
            }
            catch (System.InvalidOperationException e)
            {
                error = e.Message;
            }
            if (GenerateCompeleted != null)
            {
                Control control = GenerateCompeleted.Target as Control;
                if (control != null)
                    control.Invoke(GenerateCompeleted, new object[] { code, error });
                else
                    GenerateCompeleted(code, error);
            }
        }


    }
}
